<template>
  <div>
    <!-- 列表 -->
    <ul class="TemplateStyle">
      <!-- 入库时间 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/date.png" />
          <span class="li_txt">入库时间</span>
        </div>
        <div class="label">
          <input type="text" :value="userDate" :disabled="true" />
        </div>
      </li>
      <!-- 入库物资(名称) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/birth.png" />
          <span class="li_txt">入库物资(名称)</span>
        </div>
        <div class="label">
          <input type="text" v-model="inName" />
        </div>
      </li>
      <!-- 入库物资(单位) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/order.png" />
          <span class="li_txt">入库物资(单位)</span>
        </div>
        <div class="label">
          <input type="text" v-model="inUnit" />
        </div>
      </li>
      <!-- 入库物资(数量) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/money.png" />
          <span class="li_txt">入库物资(数量)</span>
        </div>
        <div class="label">
          <input type="number" v-model="inNum" />
        </div>
      </li>
      <!-- 生产日期 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/date.png" />
          <span class="li_txt">生产日期</span>
        </div>
        <div class="label">
          <van-cell
            style="height: 2rem;border-radius:.8rem;box-sizing:border-box;padding:.3rem 20%;"
            :value="date1"
            @click="show1 = true"
          />
          <van-calendar
            id="date1"
            :default-date="timeDefaultShow"
            :min-date="minDate"
            :max-date="maxDate"
            color="#71a16d"
            v-model="show1"
            @confirm="onConfirm1"
          />
        </div>
      </li>
      <!-- 有效期至 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/term.png" />
          <span class="li_txt">有效期至</span>
        </div>
        <div class="label">
          <van-cell
            style="height: 2rem;border-radius:.8rem;box-sizing:border-box;padding:.3rem 20%;"
            :value="date2"
            @click="show2 = true"
          />
          <van-calendar
            id="date2"
            :default-date="timeDefaultShow"
            :min-date="minDate"
            :max-date="maxDate"
            color="#71a16d"
            v-model="show2"
            @confirm="onConfirm2"
          />
        </div>
      </li>
      <!-- 物资单价(元) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/money.png" />
          <span class="li_txt">物资单价(元)</span>
        </div>
        <div class="label">
          <input type="number" v-model="inPrice" />
        </div>
      </li>
      <!-- 确认按钮 -->
      <button class="btn" @click="btnInstorage">确定</button>
    </ul>
    <!-- loading -->
    <div style="padding-bottom: 4rem; position:top:50%">
      <van-loading
        v-show="loading"
        style="position: absolute;top: 50%;left: 46%;"
        type="spinner"
        color="#1989fa"
      ></van-loading>
    </div>
  </div>
</template>
<script>
import moment from "moment";

export default {
  name: "ProductStorage",
  data() {
    return {
      userDate: "", // 展示给用户
      inTime: "",
      inName: "",
      inUnit: "",
      inNum: "",
      date1: "",
      date2: "",
      show1: false,
      show2: false,
      timeDefaultShow: "",
      minDate: new Date(2015, 0, 1),
      maxDate: new Date(2025, 0, 1),
      inPrice: "",
      loading: false // loading
    };
  },
  created() {
    // 自动生成日期
    let nowDate = new Date();
    this.inTime = moment(nowDate).format("YYYY-MM-DD HH:mm:ss");
    this.userDate = moment(nowDate).format("YYYY-MM-DD");
    //获取当天时间
    this.timeDefaultShow = new Date();
    this.timeDefaultShow.setMonth(new Date().getMonth());
  },
  methods: {
    // 生产日期
    formatDate1(date1) {
      return `${moment(date1).format("YYYY-MM-DD HH:mm:ss")}`;
    },
    onConfirm1(date1) {
      this.show1 = false;
      this.date1 = this.formatDate1(date1);
    },
    // 有效期至
    formatDate2(date2) {
      return `${moment(date2).format("YYYY-MM-DD HH:mm:ss")}`;
    },
    onConfirm2(date2) {
      this.show2 = false;
      this.date2 = this.formatDate2(date2);
    },
    btnInstorage() {
      this.loading = true; // loading显示
      let params = {
        token: JSON.parse(localStorage.getItem("login")).res.token,
        userName: JSON.parse(localStorage.getItem("login")).res.user.userName,
        inTime: this.inTime,
        inName: this.inName,
        inUnit: this.inUnit,
        inNum: this.inNum,
        produceTime: this.date1,
        effectTime: this.date2,
        inPrice: this.inPrice
      };
      this.$api
        .requestProduceProductIn(params)
        .then(res => {
          this.loading = false; // loading隐藏
          this.$dialog.alert({
            message: res
          });
        })
        .catch(err => {
          this.loading = false; // loading隐藏
          this.$dialog.alert({
            message: err
          });
        });
    }
  }
};
</script>
<style>
@import "../../assets/css/TemplateStyle.css";
</style>
